package org.sg20.graphviewer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.sg20.labelpropagation.lpaiter.LpaIterDriver;
import org.sg20.labelpropagation.lpaiter.LpaIterMapper;
import org.sg20.labelpropagation.lpaiter.LpaIterReducer;

import java.io.IOException;

public class GraphViewerDriver {
    public  static  void run(String graphFile,String outPath) throws IOException, InterruptedException, ClassNotFoundException{
        Configuration conf = new Configuration();
        conf.set("mapreduce.output.textoutputformat.separator", ",");
//        conf.set("fs.defaultFS",fsName);

        FileSystem fs=FileSystem.get(conf);
        if(fs.exists(new Path(outPath)))
            fs.delete(new Path(outPath), true);

        Job job = Job.getInstance(conf);
        job.setJobName("graph view");
        job.setInputFormatClass(KeyValueTextInputFormat.class);

        // 2 关联本Driver程序的jar
        job.setJarByClass(GraphViewerDriver.class);

        // 3 关联Mapper和Reducer的jar
        job.setMapperClass(GraphViewerMapper.class);  //和center共用一个mapper
        job.setReducerClass(GraphViewerReducer.class);


        // 4 设置Mapper输出的kv类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(DoubleWritable.class);

        // 5 设置最终输出kv类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(DoubleWritable.class);

        // 6 设置输入和输出路径
        FileInputFormat.setInputPaths(job, new Path(graphFile));
        FileOutputFormat.setOutputPath(job, new Path(outPath));

        // 7 提交job
        job.waitForCompletion(true);
        //return DigestUtils.md5Hex(fs.open(new Path(outPath+"/part-r-00000")));
    }

    public  static void main(String args[]) throws IOException, InterruptedException, ClassNotFoundException {
        run(args[0],args[1]);
    }
}
